package com.paas.form.controller;

import com.paas.form.entity.FormApiConfigEntity;
import com.paas.form.entity.FormApiConfigParametersEntity;
import com.paas.form.entity.FormInterfaceConfigParametersEntity;
import com.paas.form.service.IFormApiConfigParametersService;
import com.paas.form.service.IFormApiConfigService;
import com.paas.form.service.IFormInterfaceConfigParametersService;
import com.paas.form.vo.MdmInterfaceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.util.List;
import java.util.Date;
import com.paas.common.model.BaseUserInfo;
import com.paas.common.web.SystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.paas.common.util.BeanUtil;
import com.paas.common.model.RestResponse;
import com.paas.common.web.annotion.OperLog;
import com.paas.common.web.controller.BaseController;
import com.paas.common.web.oper.OprLogConstant;

@RestController
@RequestMapping(value = "/form/formApiConfig")
@Api(description = "cc", tags = "")
public class FormApiConfigController extends BaseController {

    @Autowired
    public IFormApiConfigService formApiConfigService;

    @Autowired
    public IFormApiConfigParametersService formApiConfigParametersService;

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ApiOperation(value = "保存信息", notes = "保存信息")
    @OperLog(operType = OprLogConstant.ADD,operModul = "",operDescribe = "-保存")
    public RestResponse save(@RequestBody MdmInterfaceVo mdmInterfaceVo) throws ParseException{
		 BaseUserInfo baseUserInfo = getUserInfo();
        List<FormApiConfigParametersEntity> formApiConfigParametersList = mdmInterfaceVo.getApiParamList();
        FormApiConfigEntity formApiConfigEntity = mdmInterfaceVo.getFormApiConfigEntity();
        if(BeanUtil.isNotEmpty(formApiConfigEntity)){
    		formApiConfigEntity.setStatus(SystemConst.NORMAL_STATUS);
            formApiConfigEntity.setCreateOrgId(baseUserInfo.getOrgId());
            formApiConfigEntity.setModifyId(baseUserInfo.getUserId());
            formApiConfigEntity.setCreateId(baseUserInfo.getUserId());
			injectUpdateDataAuth(formApiConfigEntity);
            formApiConfigEntity.setModifyDate(new Date());

        }
        formApiConfigService.save(formApiConfigEntity);

        // 删除已存在的请求参数
        FormApiConfigParametersEntity parametersEntity = new FormApiConfigParametersEntity();
        parametersEntity.setParentId(formApiConfigEntity.getId());
        List<FormApiConfigParametersEntity> parametersEntityList =   formApiConfigParametersService.getList(parametersEntity);
        if (parametersEntityList.size()>0){
            for (FormApiConfigParametersEntity  params:parametersEntityList){
                formApiConfigParametersService.deleteByParentId(params.getParentId());
            }
        }

        // 保存请求参数
        if (formApiConfigParametersList.size() > 0) {
            for (FormApiConfigParametersEntity formApiConfigParametersEntity : formApiConfigParametersList) {
                formApiConfigParametersEntity.setParentId(formApiConfigEntity.getId());
                formApiConfigParametersEntity.setCreateOrgId(baseUserInfo.getOrgId());
                formApiConfigParametersEntity.setCreateId(baseUserInfo.getUserId());
                formApiConfigParametersEntity.setStatus(SystemConst.NORMAL_STATUS);
            }
            formApiConfigParametersService.saveBatch(formApiConfigParametersList);
        }
        return RestResponse.success();
    }

    @RequestMapping(value = "/saveBatch",method = RequestMethod.POST)
    @ApiOperation(value = "批量保存信息", notes = "批量保存信息")
    @OperLog(operType = OprLogConstant.UPDATE,operModul = "",operDescribe = "-批量保存修改")
    public void saveBatch(@RequestBody List map) throws ParseException{
    	List list = BeanUtil.mapsToList(map, FormApiConfigEntity.class);
    	formApiConfigService.saveBatch(list);
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    @ApiOperation(value = "根据Id删除数据", notes = "根据Id删除数据")
    @OperLog(operType = OprLogConstant.DELETE,operModul = "",operDescribe = "-删除")
    public void delectById(String id){
		if (null != id && !"".equals(id)) {
			FormApiConfigEntity formApiConfigEntity = new FormApiConfigEntity();
			formApiConfigEntity.setId(id);
			injectUpdateDataAuth(formApiConfigEntity);
			formApiConfigService.deleteByEntity(formApiConfigEntity);
		}
    }

    @RequestMapping(value = "/deleteBatchByIds",method = RequestMethod.POST)
    @ApiOperation(value = "根据Id批量删除数据", notes = "根据Id批量删除数据")
    @OperLog(operType = OprLogConstant.DELETE,operModul = "",operDescribe = "-批量删除")
    public void deleteBatchByIds(@RequestBody List ids) {
		formApiConfigService.deleteBatchIds(ids);
    }

    @RequestMapping(value = "/queryById",method = RequestMethod.GET)
    @ApiOperation(value = "根据Id查询对象", notes = "根据Id查询对象")
    public MdmInterfaceVo queryById(String id){
        MdmInterfaceVo mdmInterfaceVo = new MdmInterfaceVo();
        FormApiConfigEntity bean = formApiConfigService.getById(id);
        if (BeanUtil.isNotEmpty(bean)){
            mdmInterfaceVo.setFormApiConfigEntity(bean);
            FormApiConfigParametersEntity toolsInterfaceConfigHeaderEntity = new FormApiConfigParametersEntity();
            toolsInterfaceConfigHeaderEntity.setParentId(bean.getId());
            List<FormApiConfigParametersEntity> headerList = formApiConfigParametersService.getList(toolsInterfaceConfigHeaderEntity);
            mdmInterfaceVo.setApiParamList(headerList);
        }
        return mdmInterfaceVo;
    }

    @RequestMapping(value = "/queryBatchByIds",method = RequestMethod.POST)
    @ApiOperation(value = "根据Id查询对象列表", notes = "根据Id查询对象列表")
    public List queryBatchByIds(@RequestBody List ids){
         List list = formApiConfigService.getBatchByIds(ids);
         return list ;
    }

    @RequestMapping(value = "/queryPage",method = RequestMethod.GET)
    @ApiOperation(value = "模板分页查询", notes = "模板分页查询")
    public IPage queryPage(FormApiConfigEntity formApiConfigEntity, Integer currentPage, Integer pageSize){
    	if (currentPage == null) {
    		currentPage = 1;
    	}
    	if (pageSize == null) {
    		pageSize = 10;
    	}
		injectUpdateDataAuth(formApiConfigEntity);
		IPage<FormApiConfigEntity> pageBean = formApiConfigService.getPage(formApiConfigEntity, currentPage, pageSize);
    	return pageBean;
    }

}